package ui.swing.table.sizedcolumns;

import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;

import ui.swing.common.SwingUtil;


public class App {
	final static Object[][] TABLE_DATA = {
			{ new Integer(1), "ONJava", "http://www.onjava.com/" },
			{ new Integer(2), "Joshy's Site", "http://www.joshy.org/" },
			{ new Integer(3), "Anime Weekend Atlanta",
					"http://www.awa-con.com/" },
			{ new Integer(4), "QTJ book",
					"http://www.oreilly.com/catalog/quicktimejvaadn/" } };

	final static String[] COLUMN_NAMES = { "Count", "Name", "URL" };

	public static void main(String[] args) {
		// 142 mac l&f has a header bug - force metal for today
		try {
			UIManager.setLookAndFeel(UIManager
					.getCrossPlatformLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
		DefaultTableModel mod = new DefaultTableModel(TABLE_DATA, COLUMN_NAMES);
		JTable table = new JTable(mod);
		JScrollPane pane =

		new JScrollPane(table, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		JFrame frame = new JFrame("JTable Column Widths");
		frame.getContentPane().add(pane);
		frame.pack();
		frame.setVisible(true);
		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

		try {
			Thread.sleep(2000);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// now get smart about col widths
		final JTable fTable = table;
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				SwingUtil.adjustColumnPreferredWidths(fTable);
				fTable.revalidate();
			}
		});
		
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				SwingUtil.runderAlternateColorTable(fTable, new Color[]{Color.GREEN, Color.YELLOW});
			}
		});
	}

}
